extern crate chrono;
use chrono::prelude::*;
use std::fs::File;
use std::io::{Result, Write};

fn main() {
    println!("cargo:rerun-if-env-changed=USER_IMG");
    if let Ok(user_img) = std::env::var("USER_IMG") {
        println!("cargo:rerun-if-changed={}", user_img);
    }
    gen_link_user_asm().unwrap();
}

/// Generate assembly file for linking user image
fn gen_link_user_asm() -> Result<()> {
    let mut f = File::create("src/link_user.S").unwrap();
    let user_img = std::env::var("USER_IMG").unwrap();
    let local: DateTime<Local> = Local::now();

    writeln!(f, "# generated by build.rs - do not edit")?;
    // 如果不加上时间戳，由于link_user.S没有改变，按照Rust增量更新机制，用户程序镜像不会更新
    writeln!(f, "# last modified at: {}", local.to_string())?;
    writeln!(
        f,
        r#"
    .section .data
    .global _user_img_start
    .global _user_img_end
_user_img_start:
    .incbin "{}"
_user_img_end:
"#,
        user_img
    )?;
    Ok(())
}
